package cn.wgx.springsecuritystudy.config.security;


import cn.wgx.springsecuritystudy.dao.Permission;
import cn.wgx.springsecuritystudy.dao.Role;
import cn.wgx.springsecuritystudy.dao.User;
import cn.wgx.springsecuritystudy.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

@Component("userDetailService")
public class MyUserDetailService implements UserDetailsService {

    @Autowired
    private UserDao userDao;

    /**
     * 构造函数,初始化admin账户和权限
     * @param userDao
     */
    public MyUserDetailService(UserDao userDao){
        UserDetails admin = userDao.findUserByUsername("admin");
        if(admin == null){
            User user_admin = new User();
            user_admin.setUsername("admin");
            user_admin.setPassword(new BCryptPasswordEncoder().encode("123123ww"));
            user_admin.setAccountNonExpired(true);
            user_admin.setAccountNonLocked(true);
            user_admin.setCredentialsNonExpired(true);
            user_admin.setEnabled(true);

            User user2 = new User();
            user2.setUsername("user");
            user2.setPassword(new BCryptPasswordEncoder().encode("123123ww"));
            user2.setAccountNonExpired(true);
            user2.setAccountNonLocked(true);
            user2.setCredentialsNonExpired(true);
            user2.setEnabled(true);

            Role r1 = new Role();
            r1.setRole_key("ROLE_admin");
            r1.setRole_title("管理员");

            Role r2 = new Role();
            r2.setRole_key("ROLE_user");
            r2.setRole_title("用户");

            Permission p1 = new Permission();
            Permission p2 = new Permission();
            p1.setPer_title("管理界面");
            p1.setPer_url("/admin");
            p2.setPer_title("用户界面");
            p2.setPer_url("/user");

            ArrayList adminPer = new ArrayList();
            adminPer.add(p1);
            adminPer.add(p2);
            r1.setPermissions(adminPer);

            ArrayList userPer = new ArrayList();
            userPer.add(p2);
            r2.setPermissions(userPer);

            ArrayList adminRole = new ArrayList();
            adminRole.add(r1);
            adminRole.add(r2);

            ArrayList userRole = new ArrayList();
            userRole.add(r2);

            user_admin.setRoles(adminRole);
            user2.setRoles(userRole);

            ArrayList users = new ArrayList();
            users.add(user_admin);
            users.add(user2);
//            userDao.save(user_admin);
            userDao.saveAll(users);


        }
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User userByUsername = userDao.findUserByUsername(username);
        if(null == userByUsername){
            throw new UsernameNotFoundException("用户名错误");
        }
        return userByUsername;
    }


}
